package br.com.salao.ejb.dao.impl;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import br.com.salao.ejb.dao.RatingEstabelecimentoDao;
import br.com.salao.entity.RatingEstabelecimento;

public class RatingEstabelecimentoDaoImpl implements RatingEstabelecimentoDao {	
	
	private final GenericDao<RatingEstabelecimento> dao;
	private EntityManager em;
	private Logger logger = Logger.getLogger(RatingEstabelecimentoDaoImpl.class.getName());

	
	public RatingEstabelecimentoDaoImpl(EntityManager em){
		this.em = em;
		dao = new GenericDao<RatingEstabelecimento>(em,RatingEstabelecimento.class);
	}	
	
	public RatingEstabelecimento inserir(RatingEstabelecimento ratingEstabelecimento) {		
		return dao.salvar(ratingEstabelecimento);
	}

	public RatingEstabelecimento update(RatingEstabelecimento ratingEstabelecimento ) {
		return dao.update(ratingEstabelecimento);
	}	
	
	public RatingEstabelecimento buscarRatingPorId(int idRating){		
	
		logger.info(":: RatingEstabelecimentoDaoImpl :: buscarRatingPorId () :: ID Rating: [" + idRating +"]");

		try {		
			Query query = em.createQuery("from RatingEstabelecimento r where r.id = :idRating");
			query.setParameter("idRating", idRating);
			return  (RatingEstabelecimento) query.getSingleResult();	
			
		} catch (Exception e) {
			
			StringBuffer sb = new StringBuffer();
			sb.append(e.getMessage() + "\n");
			logger.error(sb.toString());
			
			return null;
		} 		
	}	
}
